package com.example.controller;

import com.example.common.Result;
import com.example.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/files")
@Tag(name = "文件管理", description = "文件上传相关接口")
public class FileController {

    private final FileService fileService;

    @Autowired
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    /**
     * 文件上传接口
     *
     * @param file 上传的文件
     * @return 文件访问路径
     */
    @PostMapping("/upload")
    @Operation(summary = "文件上传", description = "上传文件到服务器")
    @ApiResponse(responseCode = "200", description = "上传成功", 
                content = @Content(mediaType = "application/json", 
                schema = @Schema(implementation = Result.class)))
    public Result upload(
            @Parameter(description = "上传的文件", required = true) 
            @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("上传的文件不能为空");
        }
        
        try {
            String fileUrl = fileService.upload(file);
            return Result.success(fileUrl);
        } catch (Exception e) {
            return Result.error("文件上传失败: " + e.getMessage());
        }
    }
} 